<?php


namespace app\api\controller;

use app\api\business\User as UserBus;
use app\api\validate\User as UserValidate;

class User extends AuthBase
{
    public function index(){
        $user = (new UserBus())->getNormalUserById($this->userId);
        $result = [
            'id' => $this->userId,
            'username' => $user['username'],
            'sex' => $user['sex']
        ];
        return $this->show(config('status.success'),'OK',$result);
    }

    public function update(){
        $username = input('param.username','','trim');
        $sex = input('param.sex',0,'intval');

        $data = [
            'username' => $username,
            'sex' => $sex
        ];
        $validate = (new UserValidate())->scene('update_user');
        if (!$validate->check($data)){
            return show(config('status.error'),$validate->getError());
        }
        $userBusObj = new UserBus();
        $user = $userBusObj->update($this->userId,$data);
        if (!$user){
            return show(config('status.error'),'更新失败');
        }
        return  show('status.success','更新成功');
    }
}